﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SetTimeDialog.cs" company="YellowFish">
//   Copyright © YellowFish 2009
// </copyright>
// <summary>
//   Defines the SetTimeDialog type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using Diagnostics.Annotations;

namespace MobilePal.Dialogs
{
  using System;
  using System.Diagnostics;
  using System.Drawing;
  using System.Globalization;
  using System.Windows.Forms;

  using Controls;

  /// <summary>
  /// Describes and manipulates the set time dialog.
  /// </summary>
  public partial class SetTimeDialog: Form
  {
    #region Constants and Fields


    private CultureInfo _cultureInfo = CultureInfo.GetCultureInfo("en-us");
    private string _dateFormatString = "dddd dd MMMM";


    #endregion


    #region Constructors and Destructors


    /// <summary>
    /// Initializes a new instance of the <see cref="SetTimeDialog"/> class.
    /// </summary>
    public SetTimeDialog()
    {
      InitializeComponent();

      timeSlider.ValueChanged += TimeSliderValueChanged;
      UpdateDateLabel();

      UpdateSliderPosition();
    }


    #endregion


    #region Properties


    /// <summary>
    /// Gets or sets the culture info.
    /// </summary>
    /// <value>
    /// The culture info.
    /// </value>
    [NotNull]
    public CultureInfo CultureInfo
    {
      get
      {
        return _cultureInfo;
      }

      set
      {
        Assert.ArgumentNotNull(value, "value");

        _cultureInfo = value;
        UpdateDateLabel();
      }
    }


    /// <summary>
    /// Gets or sets the date format string.
    /// </summary>
    /// <value>
    /// The date format string.
    /// </value>
    [NotNull]
    public string DateFormatString
    {
      get
      {
        return _dateFormatString;
      }

      set
      {
        Assert.ArgumentNotNull(value, "value");

        _dateFormatString = value;
        UpdateDateLabel();
      }
    }


    /// <summary>
    /// Gets or sets the max value.
    /// </summary>
    /// <value>
    /// The max value.
    /// </value>
    public DateTime MaxValue
    {
      get
      {
        return timeSlider.MaxValue;
      }

      set
      {
        timeSlider.MaxValue = value;
      }
    }

    /// <summary>
    /// Gets or sets a value indicating whether the max value is now.
    /// </summary>
    /// <value>
    /// Returns <c>true</c> if max value is now; otherwise, <c>false</c>.
    /// </value>
    public bool MaxValueIsNow
    {
      get;
      set;
    }


    /// <summary>
    /// Gets or sets the min value.
    /// </summary>
    /// <value>
    /// The min value.
    /// </value>
    public DateTime MinValue
    {
      get
      {
        return timeSlider.MinValue;
      }

      set
      {
        timeSlider.MinValue = value;
      }
    }

    /// <summary>
    /// Gets or sets a value indicating whether the min value is now.
    /// </summary>
    /// <value>
    /// Returns <c>true</c> if min value is now; otherwise, <c>false</c>.
    /// </value>
    public bool MinValueIsNow
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the slide distance.
    /// </summary>
    /// <value>
    /// The slide distance.
    /// </value>
    public int SlideDistance
    {
      get
      {
        return timeSlider.SlideDistance;
      }

      set
      {
        timeSlider.SlideDistance = value;
      }
    }


    /// <summary>
    /// Gets or sets the color of the thumbs background.
    /// </summary>
    /// <value>
    /// The color of the thumbs background.
    /// </value>
    public Color ThumbsBackgroundColor
    {
      get
      {
        return timeSlider.ThumbsBackgroundColor;
      }

      set
      {
        timeSlider.ThumbsBackgroundColor = value;
      }
    }


    /// <summary>
    /// Gets or sets the color of the thumbs down.
    /// </summary>
    /// <value>
    /// The color of the thumbs down.
    /// </value>
    public Color ThumbsDownColor
    {
      get
      {
        return timeSlider.ThumbsDownColor;
      }

      set
      {
        timeSlider.ThumbsDownColor = value;
      }
    }

    /// <summary>
    /// Gets or sets the thumbs padding.
    /// </summary>
    /// <value>
    /// The thumbs padding.
    /// </value>
    public int ThumbsPadding
    {
      get
      {
        return timeSlider.ThumbsPadding;
      }

      set
      {
        timeSlider.ThumbsPadding = value;
      }
    }


    /// <summary>
    /// Gets or sets the value.
    /// </summary>
    /// <value>
    /// The value.
    /// </value>
    public DateTime Value
    {
      get
      {
        return timeSlider.Value;
      }

      set
      {
        timeSlider.Value = value;
      }
    }


    #endregion


    #region Methods


    /// <summary>
    /// Raises the <see cref="E:System.Windows.Forms.Form.Closed"/> event.
    /// </summary>
    /// <param name="e">
    /// The <see cref="T:System.EventArgs"/> that contains the event data.
    /// </param>
    protected override void OnClosed([NotNull] EventArgs e)
    {
      Assert.ArgumentNotNull(e, "e");
    }


    /// <summary>
    /// Raises the <see cref="E:System.Windows.Forms.Control.Resize"/> event.
    /// </summary>
    /// <param name="e">
    /// An <see cref="T:System.EventArgs"/> that contains the event data.
    /// </param>
    protected override void OnResize([NotNull] EventArgs e)
    {
      Assert.ArgumentNotNull(e, "e");

      UpdateSliderPosition();
    }


    private void CancelMenuItemClick([NotNull] object sender, [NotNull] EventArgs e)
    {
      Assert.ArgumentNotNull(sender, "sender");
      Assert.ArgumentNotNull(e, "e");

      DialogResult = DialogResult.Cancel;
    }


    private void OkMenuItemClick([NotNull] object sender, [NotNull] EventArgs e)
    {
      Assert.ArgumentNotNull(sender, "sender");
      Assert.ArgumentNotNull(e, "e");

      DialogResult = DialogResult.OK;
    }


    private void ResetMenuItemClick([NotNull] object sender, [NotNull] EventArgs e)
    {
      Assert.ArgumentNotNull(sender, "sender");
      Assert.ArgumentNotNull(e, "e");

      timeSlider.Value = DateTime.Now;
    }


    private void TimeSliderValueChanged([NotNull] object sender, [NotNull] UpDownSliderEventArgs<DateTime> e)
    {
      Assert.ArgumentNotNull(sender, "sender");
      Assert.ArgumentNotNull(e, "e");

      if (MinValueIsNow)
      {
        timeSlider.MinValue = DateTime.Now;
      }

      if (MaxValueIsNow)
      {
        timeSlider.MaxValue = DateTime.Now;
      }

      UpdateDateLabel();
    }


    private void UpdateDateLabel()
    {
      dateLabel.Text = timeSlider.Value.ToString(_dateFormatString, _cultureInfo);
    }


    private void UpdateSliderPosition()
    {
      int x = (Width - timeSlider.Width) / 2;
      int y = (Height - timeSlider.Height) / 2;

      timeSlider.Location = new Point(x, y);
    }


    #endregion
  }
}